class BlogsController < ApplicationController
  before_filter :find_user, :except => [ :index ]
  before_filter :find_blog, :except => [ :index, :new, :create ]
  before_filter :login_required, :except => [ :index, :show ]
  # GET /users/1/blogs
  # GET /users/all/blogs
  # GET /users/1/blogs.xml
  # GET /users/all/blogs.xml
  def index
    @user = User.find(params[:user_id]) unless params[:user_id] == 'all'
    if params[:user_id] == 'all' 
      @blogs = Blog.find(:all, :order => "updated_at DESC")
    else  
      @blogs = Blog.find(:all, :conditions => ["user_id = ?", @user], :order => "updated_at DESC")
    end  

    respond_to do |format|
      format.html # index.rhtml
      format.xml  { render :xml => @blogs.to_xml }
    end
  end

  # GET /users/1/blogs/1
  # GET /users/1/blogs/1.xml
  def show
    respond_to do |format|
      format.html {
        @pager = ::Paginator.new(Article.count_per_user_blog(@user, params[:id]), AppConstants::LIMIT) do |offset, per_page|
          Article.find_per_user_blog(@user, params[:id], offset, per_page)
        end
        @num_pages = Article.count_per_user_blog(@user, params[:id])/AppConstants::LIMIT + 1
        @page = @pager.page(params[:page])
      }                
      format.xml  { 
        offset = (params[:page] and params[:page].to_i > 1) ?
                 (params[:page].to_i - 1)*AppConstants::LIMIT : 0
        @articles = Article.find_per_user_blog(@user, params[:id], offset, AppConstants::LIMIT)
        render :xml => @articles.to_xml(:except => [:body]) 
      }
    end
  end

  # GET /users/1/blogs/new
  def new
    @blog = Blog.new
  end

  # GET /users/1/blogs/1;edit
  def edit
  end

  # POST /users/1/blogs
  # POST /users/1/blogs.xml
  def create
    @blog = Blog.new(params[:blog])
    @blog.user = current_user

    respond_to do |format|
      if @blog.save
        flash[:notice] = 'Blog was successfully created.'
        format.html { redirect_to blogs_url(@user) }
        format.xml  { head :created, :location => blog_url(@user, @blog) }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @blog.errors.to_xml }
      end
    end
  end

  # PUT /users/1/blogs/1
  # PUT /users/1/blogs/1.xml
  def update
    respond_to do |format|
      if @blog.update_attributes(params[:blog])
        flash[:notice] = 'Blog was successfully updated.'
        format.html { redirect_to blog_url(@user, @blog) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @blog.errors.to_xml }
      end
    end
  end

  # DELETE /users/1/blogs/1
  # DELETE /users/1/blogs/1.xml
  def destroy
    @blog.destroy

    respond_to do |format|
      format.html { redirect_to blogs_url(@user) }
      format.xml  { head :ok }
    end
  end

  def delete
  end

protected
  def find_user
    @user = User.find(params[:user_id])
  end  

  def find_blog
    @blog = Blog.find(params[:id])
  end  

  # all logged in users can have blogs
  # only blog author and admin can modify/delete it
  def authorized?
    return true if ['index', 'new', 'create' ].include?(action_name)
    current_user == @blog.user || current_user.login == 'admin'
  end
end
